//
//  CreatWishTableViewController.m
//  ProjectB
//
//  Created by lanou on 16/5/5.
//  Copyright © 2016年 lanou. All rights reserved.
//

#define KWidth self.view.frame.size.width

#import "CreatWishTableViewController.h"
#import "CreatWishTableViewCell.h"

@interface CreatWishTableViewController ()<UISearchResultsUpdating>
//搜索框
@property (nonatomic, strong) UISearchController *searchController;
//保存原有数据
@property (nonatomic, strong) NSArray *dataArray;
//保存筛选出来的数据
@property (nonatomic, retain) NSMutableArray *searchArray;

@end

@implementation CreatWishTableViewController
- (NSArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSArray array];
    }
    return _dataArray;
}
- (NSMutableArray *)searchArray {
    if (!_searchArray) {
        _searchArray = [NSMutableArray array];
    }
    return _searchArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.items.firstObject.backBarButtonItem = [UIBarButtonItem createBackItem];
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    
    self.navigationItem.title = @"创建心愿单";
    //导航栏的半透明效果
    self.navigationController.navigationBar.translucent = NO;
    
    self.dataArray = @[@"不能说的秘密",@"性别男，爱好女",@"想象中未来恋人的样子",@"这辈子一定要去的地方",@"成年前要做的那些事",@"想看的电影清单",@"想要追的那个人",@"给前任的便签"];
    
    [self creatSearchController];
    
    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"CreatWishTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
}

//创建搜索框
- (void)creatSearchController {
    //searchResultsController参数设置为nil,能在相同的视图中显示搜索结果
    _searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchBar.frame = CGRectMake(0, 0, KWidth, 40);
    //设置代理 搜索数据更新
    self.searchController.searchResultsUpdater = self;
    //遮盖层
//    self.searchController.dimsBackgroundDuringPresentation = YES;
    //当点击输入框的时候导航视图控制器的显隐问题
    self.searchController.hidesNavigationBarDuringPresentation = NO;
    
    //弹出键盘的样式
    self.searchController.searchBar.keyboardType = UIKeyboardTypeDefault;
    //输入框的占位符
    self.searchController.searchBar.placeholder = @"心愿单标题";
    self.searchController.searchBar.barTintColor = [UIColor lightGrayColor];
    self.tableView.tableHeaderView = self.searchController.searchBar;

    
}

#pragma mark-------SearchViewController协议方法-------------

//必须实现的方法
//使用UISearchResultsUpdating协议更新搜索结果根据用户输入的信息搜索栏。
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    
    //把SearchBar上面的Cancel按钮改成中文
    searchController.searchBar.showsCancelButton = YES;
    UIButton *cancelButton = [[UIButton alloc] init];
    UIView *topView = self.searchController.searchBar.subviews[0];
    for (UIView *subView in topView.subviews) {
        if ([subView isKindOfClass:[UIButton class]]) {
            cancelButton = (UIButton*)subView;
            [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
            
        }
    }
    //获取在输入框输入的值
    NSString *searchString = [self.searchController.searchBar text];
    //self表示要查询集合中对象，contain[c]表示包含字符串，其中c是不区分大小写
    NSPredicate *predicate =[NSPredicate predicateWithFormat:@"self contains[c] %@", searchString];
    if (self.searchArray != nil) {
        [self.searchArray removeAllObjects];
    }
    self.searchArray = [NSMutableArray arrayWithArray:[self.dataArray filteredArrayUsingPredicate:predicate]];
    
    [self.tableView reloadData];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (self.searchController.active) {
        return _searchArray.count;
    }else{
        return _dataArray.count;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     CreatWishTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    if (self.searchController.active) {
        cell.textLabel.text = _searchArray[indexPath.row];
    }else {
        cell.textLabel.text = _dataArray[indexPath.row];
    }
     
    //箭头辅助视图
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    // Configure the cell...
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
